<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/20
 * Time: 8:45
 */

namespace app\index\controller;
use app\api\model\RoomOrdersModal;
use app\common\controller\BaseController;
use app\index\model\AppUserModel;
use app\index\model\RoomsModel;
use app\index\model\UserRoomModel;
use app\index\model\PoliceStationModel;
use think\Validate;

class Room extends BaseController
{
    public function roomInfo(){
        return $this->authAjaxReturn(200,null,"success");
    }

    //新增数据
    public function save(){
        $account = input("post.account");
        $room_code = input("post.room_code");
        $room_address = input("post.room_address");
        $room_num = input("post.room_num");
        $police_station_id = input("post.police_station_id");
        $room_detail = input("post.room_detail");
        $data = [
            "room_code" => $room_code,
            "room_address" => $room_address,
            "room_num" => $room_num,
            "room_detail" => $room_detail,
        ];
        $rule = [
            "room_code" => 'require|number',
            "room_address" => 'require|chsAlphaNum',
            "room_num" => 'require|number',
            "room_detail" => 'max:255',
        ];
        $msg = [
            'room_code.require' => '房间号不能为空',
            'room_code.number'   => '房间号只能为数字',
            'room_address.require' => '住房地址不能为空',
            'room_address.chsAlphaNum'   => '住房地址只能为汉字、字母或数字',
            'room_num.require' => '可住房间数不能为空',
            'room_num.number'   => '可住房间数只能为数字',
            'room_detail.max' => '房间描述最多不能超过255个字符',

        ];
        $validate = new Validate($rule,$msg);
        if(!$validate->check($data)){
            return $this->ajaxReturn(600,null,$validate->getError());
        }
        $model = new RoomsModel();
        $model->room_code = $room_code;
        $model->room_address = $room_address;
        $model->room_num = $room_num;
        $model->room_detail =$room_detail;
        $model->police_station_id = $police_station_id;

        $data = $model->where("room_address",$room_address)->select();
        if($data){
            return $this->ajaxReturn(400,$data,"所住地址已存在");
        }else{
            $model->save();
            $room = $model->where('room_code',$room_code)->find();
            $room_id = $room['id'];
            if($account){
                $appUsers = new AppUserModel();
                $map['account']=$account;
                $map['type']=1;
                $id=$appUsers->where($map)->find();
                $uid = $id['id'];
                $user_room_model = new UserRoomModel();
                $user_room_model->uid=$uid;
                $user_room_model->room_id =$room_id;
                $user_room_model->save();
            }
            return $this->ajaxReturn(200,null,"添加成功");
        }
    }

    //查询数据
    public function select(){
        $police_query = input('get.police_query');
        $account = input('get.account');
        $checkbox = input('get.checkbox');
        $page = input('get.page',1);
        $limit = input('get.limit',10);
        $having = '1=1';
        $map = array();
        $model = new RoomsModel();
        if(!empty($account)){
            $map['c.account'] = ['like','%'.$account.'%'];
        }
        if(!empty($police_query)){
            $map['e.province|e.city|e.region|e.policeStation'] =  ['like','%'.$police_query.'%'];
        }
        if ($checkbox == "true"){
            $having .= ' and b.room_num>sum';
        }
        $data = $model
            ->alias('b')
            ->join('think_user_room a','a.room_id = b.id')
            ->join('think_police_station e','e.id = b.police_station_id')
            ->join('think_app_users c','c.id = a.uid')
            ->join('think_room_orders d','b.room_code = d.room_code and d.status=1','left')
            ->group('b.room_code')
            ->field('b.*,c.account,ifnull(sum(d.room_number),0) as sum')
            ->where($map)
            ->having($having)
            ->page($page,$limit)
            ->order('b.create_time desc')
            ->select();
        if($data){
            return $this->ajaxReturn(200,$data,'获取成功');
        }
        else{
            return $this->ajaxReturn(400,$data,'获取失败');

        }
    }

    public function query(){
        $isNew = input("post.isNew");
        $region = input("post.region");
        $province = input("post.province");
        $city = input('post.city');
        $model = new PoliceStationModel();
        $model->region=$region;
        if($isNew){
            $data=$model->distinct('province')
                ->field('province')
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }else if($province){
            $data=$model->where('province',$province)
                ->distinct('city')
                ->field('city')
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }else if($city){
            $data=$model->where('city',$city)
                ->distinct('region')
                ->field('region')
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }else{
            $data=$model->alias('a')
                ->where('region',$region)
                ->select();
            return $this->ajaxReturn(200,$data,"success");
        }
    }

    public function roomQuery(){
        $page = input("get.page",1);
        $limit = input("get.limit",5);
        $model = new RoomsModel();
        $data = $model
            ->alias('a')
            ->join('think_user_police_station b','a.police_station_id = b.police_station_id')
            ->join('think_police_station e','b.police_station_id = e.id')
            ->field('a.*,b.*,e.*')
            ->page($page,$limit)->select();
        return $this->ajaxReturn(200,$data,'刷新成功');
    }

    //获取数据库总数据条数
    public function getPageLimit(){
        $police_query = input('get.police_query');
        $account = input('get.account');
        $checkbox = input('get.checkbox');
        $having = '1=1';
        $model = new RoomsModel();
        $map=array();
        if(!empty($account)){
            $map['c.account'] = ['like','%'.$account.'%'];
        }
        if(!empty($police_query)){
            $map['e.province|e.city|e.region|e.policeStation'] = ['like','%'.$police_query.'%'];
        }
        if ($checkbox == "true"){
            $having .= ' and b.room_num>sum';
        }
        $data = $model
            ->alias('b')
            ->join('think_user_room a','a.room_id = b.id','left')
            ->join('think_police_station e','e.id = b.police_station_id')
            ->join('think_app_users c','c.id = a.uid')
            ->join('think_room_orders d','b.room_code = d.room_code and d.status=1','left')
            ->group('b.room_code')
            ->field('b.*,ifnull(sum(d.room_number),0) as sum')
            ->where($map)
            ->having($having)
            ->count();
        return $this->ajaxReturn(200,$data,'获取成功');
    }

    //修改路由
    public function update(){
        $room_code = input("post.room_code");
        $room_address = input("post.room_address");
        $room_num = input("post.room_num");
        $model = new RoomsModel();
        $model->room_address = $room_address;
        $model->room_num = $room_num;
        $data = $model->where('room_code', $room_code)
            ->update([
                'room_address' => $room_address,
                'room_num' => $room_num
            ]);
        return $this->ajaxReturn(200,$data,'修改成功');
    }

    //删除路由
    public function delete(){
        $room_code = input('post.room_code');
        $model = new RoomsModel();
        $data = $model->where('room_code', $room_code)->delete();
        return $this->ajaxReturn(200,$data,'删除成功');
    }

    //获取省份
    public function get_province(){
        $model =new PoliceStationModel();
        $data = $model->distinct('province')->field('province')->select();
        return $this->ajaxReturn(200,$data,'获取成功');
    }

    //获取城市
    public function get_city(){
        $province = input('post.province');
        $model =new PoliceStationModel();
        $data = $model
            ->distinct('city')
            ->field('city')
            ->where('province',$province)
            ->select();
        return $this->ajaxReturn(200,$data,'获取成功');
    }

    //获取辖区
    public function get_region(){
        $city = input('post.city');
        $model =new PoliceStationModel();
        $data = $model
            ->distinct('region')
            ->field('region')
            ->where('city',$city)
            ->select();
        return $this->ajaxReturn(200,$data,'获取成功');
    }

    //获取分局
    public function get_policeStation(){
        $region = input('post.region');
        $model =new PoliceStationModel();
        $data = $model
            ->distinct('policeStation')
            ->field('policeStation')
            ->where('region',$region)
            ->select();
        return $this->ajaxReturn(200,$data,'获取成功');
    }

    //根据房间号查看订单信息
    public function getOrderInfo(){
        $room_code = input('post.room_code');
        $model = new RoomOrdersModal();
        $data = $model
            ->where(['room_code'=>$room_code,'status'=>1])
            ->select();
        for($i=0;$i<count($data);$i++){
            if($data[$i]->custom_sex==1){
                $data[$i]->custom_sex="男";
            }else{
                $data[$i]->custom_sex="女";
            }
        }
        if ($data){
            return $this->ajaxReturn(200,$data,'获取成功');
        }else{
            return $this->ajaxReturn(400,$data,'获取失败');
        }
    }


    public function room_query(){
        $model = new AppUserModel();
        $page = input("get.page",1);
        $limit = input("get.limit",8);
        $input_query = input('get.input_query','');
        $map['type'] = 1;
        $map['account|phone'] = ['like','%'.$input_query.'%'];

        $data = $model
            ->alias('a')
            ->page($page,$limit)
            ->order('a.create_time desc')
            ->where($map)
            ->select();
        if ($data){
            return $this->ajaxReturn(200,$data,'存在你查找的信息');
        }else{
            return $this->ajaxReturn(400,$data,'不存在你查找的信息');
        }

    }

    public function getTypePageLimit(){
        $model = new AppUserModel();
        $data = $model->where('type','=',1)->count();
        return $this->ajaxReturn(200,$data,'刷新成功');
    }

    public function getSearchPageLimit(){
        $input_query = input('get.input_query');
        $model = new AppUserModel();

        $map['type'] = 1;
        $map['account|phone'] = ['like','%'.$input_query.'%'];
        $data = $model
            ->where($map)
            ->count();
        return $this->ajaxReturn(200,$data,'刷新成功');
    }

}